namespace Xsxiaosa.Net
{
    using System;
    using System.Reflection;
    using System.Threading;

    internal class XsxiaosaAsyncResult : IAsyncResult
    {
        // Methods
        public XsxiaosaAsyncResult(object obj, string method, AsyncCallback userCallback, object stateObject, params object[] parameters)
        {
            int num1;
            this._result = null;
            this._error = null;
            this._thread = null;
            this._resetEvent = new ManualResetEvent(false);
            this._completed = false;
            this._closed = false;
            this._sync = new object();
            this._obj = obj;
            this._userCallback = userCallback;
            this._stateObject = stateObject;
            this._params = parameters;
            Type[] typeArray1 = new Type[((uint) this._params.Length)];
            for (num1 = 0; (num1 < this._params.Length); num1 += 1)
            {
                typeArray1[num1] = this._params[num1].GetType();
            }
            this._method = method;
            this._methodInfo = this._obj.GetType().GetMethod(this._method, (BindingFlags.InvokeMethod | (BindingFlags.Public | BindingFlags.Instance)), null, typeArray1, null);
            if (this._methodInfo == null)
            {
                throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString("InvalidAsyncCall"));
            }
            this._thread = new Thread(new ThreadStart(this.Start));
        }

        public object Finish(object obj, string method)
        {
            object[] objArray1;
            if ((this._method != method) || (this._obj != obj))
            {
                objArray1 = new object[1];
                objArray1[0] = method;
                throw new ArgumentException(Xsxiaosa.Net.Messages.GetString("InvalidEndCall", objArray1));
            }
            object obj2 = this._sync;
            lock (obj2)
            {
                if (this._closed)
                {
                    objArray1 = new object[1];
                    objArray1[0] = method;
                    throw new InvalidOperationException(Xsxiaosa.Net.Messages.GetString("EndAlreadyCalled", objArray1));
                }
                this._closed = true;
            }
            this._resetEvent.WaitOne();
            this._resetEvent.Close();
            this._resetEvent = null;
            object obj1 = this._result;
            Exception exception1 = this._error;
            this._stateObject = null;
            this._result = null;
            this._params = null;
            this._error = null;
            if (exception1 != null)
            {
                return exception1;
            }
            return obj1;
        }

        private void Start()
        {
            AsyncCallback callback1;
            object obj1;
            try
            {
                this._result = this._methodInfo.Invoke(this._obj, this._params);
            }
            catch (TargetInvocationException exception1)
            {
                this._error = exception1.InnerException;
            }
            catch (Exception exception2)
            {
                this._error = exception2;
            }
            finally
            {
                obj1 = this._sync;
                lock (obj1)
                {
                    this._completed = true;
                    this._resetEvent.Set();
                    callback1 = this._userCallback;
                    this._userCallback = null;
                }
                if (callback1 != null)
                {
                    callback1.Invoke(this);
                }
            }
        }


        // Properties
        public object AsyncState
        {
            get
            {
                return this._stateObject;
            }
        }

        public WaitHandle AsyncWaitHandle
        {
            get
            {
                return this._resetEvent;
            }
        }

        public bool CompletedSynchronously
        {
            get
            {
                return false;
            }
        }

        public bool IsCompleted
        {
            get
            {
//                bool flag1;
                object obj1 = this._sync;
                lock (obj1)
                {
                    return this._completed;
                }
//jyj modify                return flag1;
            }
        }

        public Thread Thread
        {
            get
            {
                return this._thread;
            }
        }


        // Fields
        private bool _closed;
        private bool _completed;
        private Exception _error;
        private string _method;
        private MethodInfo _methodInfo;
        private object _obj;
        private object[] _params;
        private ManualResetEvent _resetEvent;
        private object _result;
        private object _stateObject;
        private object _sync;
        private Thread _thread;
        private AsyncCallback _userCallback;
    }}

